#ifndef CONFIGWIFI_H
#define CONFIGWIFI_H
#include <WiFi.h> // 引入 WiFi 库
#include "../Utility/BM8563/I2C_BM8563.h"
#include <DNSServer.h>
#include <WebServer.h>
#include "HttpClient.h"
#include <ESPmDNS.h>     //用于设备域名 MDNS.begin("esp32")
#include <Preferences.h> //用于参数数据的

static String WiFi_Name = "none";
static String WiFi_Pass = "none";
class ConfigWiFi
{
public:
    bool AutoTime = true;
    bool wifiEnabled = false;
    bool OnAP = false;
    const char *AP_SSID = "Hugo@kkl-AP";
    ConfigWiFi();
    ~ConfigWiFi();
    void Init();
    void enableWiFi();
    void disableWiFi();
    bool isWiFiEnabled() const;
    bool isConnected() const;
    void APMode();
    bool APClose();
    void NtpTimeCorr();
    void WiFiloop();
    bool enableIPv6();
    char *WiFiN();
    char *WiFiMac();
    char *LocalIP();
    char *GateWay();
    String GetIPv6();

private:
    const char *HOST_NAME = "Chappie-Hugokkl";
    const char *ntpServer = "ntp.aliyun.com";
};
#define WiFi_LOG(format, args...) printf(format, ##args)

/* ----------  ---------- */

#define ROOT_HTML "<!DOCTYPE html><html lang=\"en\"><head><meta http-equiv=\"Content-Type\"content=\"text/html;charset=UTF-8\"><title>WiFi配置页面</title><meta name=\"viewport\"content=\"width=device-width,initial-scale=1.0,minimum-scale=0.3,maximum-scale=0.8,user-scalable=no\"/><style>body{color:rgba(67,67,67,0.9);background-image:linear-gradient(145deg,rgba(255,150,205,0.5),#EF96C5,#EAD6EE,#EEBD89,#F9957F);background-size:800%;animation:bganimation 15s infinite;max-width:99%;height:50em}@keyframes bganimation{0%{background-position:0%50%}50%{background-position:100%50%}100%{background-position:0%50%}}#lable{font-size:calc(3em)}#box{display:flex;align-items:center;justify-content:flex-start;flex-direction:column}div{margin:auto;left:0;top:0;right:0;bottom:0}p{line-height:0}.scan-btn{list-style:none;display:block;border:0;border-radius:25px;cursor:pointer;font-size:18px;height:50px;width:135px;line-height:45px;margin:18px auto;padding:0 30px;box-shadow:3px 3px 3px 3px rgba(141,124,124,0.2);background-color:rgba(255,255,255,0.20)}@keyframes btn_style{from{height:50px;width:135px}to{height:50px;width:50px}}#Scan_Box{height:20em;max-width:80%}#scan_list{margin-top:20px;box-sizing:border-box;border-radius:18px;width:19em;background:rgba(255,255,255,0.30);backdrop-filter:blur(1em)}#scan_list>li{line-height:32px;position:relative;cursor:pointer;display:block;padding:8px 15px;font-family:'Final Frontier'}#scan_list>li:first-child{border-top:0;border-top-left-radius:20px;border-top-right-radius:20px}#scan_list>li:last-child{border-bottom-left-radius:20px;border-bottom-right-radius:20px}#scan_list>li:hover{background:#d0d3d4}.wifi-logo{position:absolute;top:14px;height:18px}.wifi-lock{position:absolute;top:0.9em;right:1.2em;width:18px;height:18px}.encrytype{color:#999;display:inline-block;z-index:1;position:absolute;right:16px}.ssid_name{margin-left:3em;font-size:16px}#spot{background-color:#0093E9;display:none;height:50px;width:50px;border-radius:50px;animation:spot 1.2s infinite}@keyframes spot{0%{-webkit-transform:scale(0.1);transform:scale(0.1);opacity:1}50%{-webkit-transform:scale(1);transform:scale(1);opacity:0.5}100%{-webkit-transform:scale(0.1);transform:scale(0.1);opacity:1}}#password_Box{position:fixed;height:12em;width:25em;padding:1em;background:rgba(255,255,255,0.80);border-radius:2em;display:none}#inp_password{margin-left:2.2em;font-size:1.5em;border-style:none;border-radius:0.2em;margin-top:0.5em}.sumit{border-style:none;font-size:1em;margin-top:1.5em;height:2.3em;width:10.3em;margin-left:7.7em;background:#0093E9;border-radius:1em;color:#fff}.sumit:hover{background:#49b3fc;cursor:pointer}#close{margin-left:11.2em;font-size:2.0em;right:0;cursor:pointer}.tick{stroke-dasharray:320;stroke-dashoffset:300;animation:tick 2.1s forwards}#click_success{width:15em;height:18em;align-items:center;flex-direction:column;display:none}@keyframes tick{0%{stroke-dashoffset:300}50%{stroke-dashoffset:150}100%{stroke-dashoffset:0}}</style></head><body><div id=\"backgroud\"><div id=\"box\"><p id=\"lable\">WiFi配置</p><p style=\"color:#4f4f4f;text-align:center;\">当前设备类型:ESP32S3仅支持2.4G</p><p style=\"color:rgba(91,91,91,0.9);font-size:2em\">点击开始搜索WiFi</p><button style=\"color:rgb(91,91,91);\"onclick=\"scan()\"id=\"scan-btn\"class=\"scan-btn\">扫描WIFI</button><!--click--></div><div id=\"Scan_Box\"><div id=\"spot\"></div><div id=\"scan_list\"></div><!--如果传输回来成功的话显示连接成功--><div id=\"click_success\"><svg width=\"12em\"height=\"12em\"><circle fill=\"#008EFF\"stroke-opacity=\"0.5\"stroke-width=\"8\"cx=\"100\"cy=\"100\"r=\"90\"/><polyline fill=\"none\"stroke-width=\"10\"stroke=\"#fff\"class=\"tick\"points=\"52,100 92,140 157,64\"stroke-linecap=\"round\"stroke-linejoin=\"round\"/></svg><h4 style=\"line-height:0.1em\">连接成功,请手动关闭AP</h4></div></div><div id=\"password_Box\"><div id=\"close\"onclick=\"back()\">×</div><!--click--><p id=\"text\"style=\"text-align:center;font-family:'Final Frontier';font-size:1.3em;\">test</p><input type=\"password\"id=\"inp_password\"><!--<button id=\"config\"class=\"sumit\"type=\"submit\">好</button>--><button class=\"sumit\"id=\"config\">好</button><!--click--></div></div><div style=\"border:3px dashed#467eff;width:13em;text-align:center\"><p style=\"font-size:1.2em;font-family:'Final Frontier'\">Web Design By<span style=\"color:#467eff\">Yeely</span></p></div><script>let flag=0;let LockSymbol=\"<img class='wifi-lock'src=''>\";let WiFiFull=\"<img class='wifi-logo'src=''>\";let WiFiMid=\"<img class='wifi-logo'src=''>\";let WiFiLow=\"<img class='wifi-logo'src=''>\";let WiFiU=\"<img class='wifi-logo'src=''>\";let ssid_name;let password;function back(){document.getElementById(\"password_Box\").style.display=\"none\";document.getElementById(\"inp_password\").value=\"\"}function connect(){var xmlhttp=new XMLHttpRequest();var requestString;requestString=\"./configwifi?ssid=\"+ssid_name+\"&password=\"+password;xmlhttp.onreadystatechange=function(){if(xmlhttp.status==201||xmlhttp.responseText==\"false\"){document.getElementById(\"scan-btn\").style.display=\"flex\";document.getElementById(\"scan-btn\").textContent=\"连接失败\";document.getElementById(\"spot\").style.display=\"none\"}else if(xmlhttp.status==200||xmlhttp.responseText==\"true\"){document.getElementById(\"scan-btn\").textContent=\"成功\";document.getElementById('spot').style.display='none';document.getElementById(\"click_success\").style.display=\"flex\";var box=document.createElement(\"div\");box.id='Scan_Box';document.getElementById(\"backgroud\").append(box);var div=document.createElement(\"div\");div.id=\"scan_list\";document.getElementById(\"Scan_Box\").appendChild(div)}};xmlhttp.open(\"GET\",requestString,true);xmlhttp.send()}function wificonfig(name){ssid_name=name;document.getElementById(\"password_Box\").style.display=\"block\";document.getElementById(\"text\").innerHTML=\"请输入\"+ssid_name+\"的密码\";document.getElementById(\"config\").onclick=function(){password=document.getElementById('inp_password').value;if(password.length>=8){document.getElementById('scan-btn').style.display=\"none\";document.getElementById('scan_list').remove('li');document.getElementById('password_Box').style.display=\"none\";connect()}else{if(password.length>=1&&password.length<=7){alert(\"密码不低于8位\")}else{password=\"\";document.getElementById('scan-btn').style.display=\"none\";document.getElementById('scan_list').remove('li');document.getElementById('select_box_city').style.display=\"flex\";document.getElementById('password_Box').style.display=\"none\";document.getElementById('select_box_city').style.display=\"flex\"}}}}function scan(){if(flag>=1){document.getElementById('scan_list').remove();var div=document.createElement('div');div.id='scan_list';document.getElementById('Scan_Box').appendChild(div)}var btn=document.getElementById('spot');btn.style.display='block';document.getElementById('scan-btn').setAttribute(\"disabled\",\"true\");document.getElementById('scan-btn').textContent=\"扫描中\";document.getElementById('scan-btn').style.opacity=\"0.7\";var xml=new XMLHttpRequest();xml.onreadystatechange=function(){if(xml.readyState==4&&xml.status==200){json=JSON.parse(xml.responseText);jjj:for(var i=0;i<json.req.length;i++){for(let j=0;j<json.req.length;j++){if(json.req[i].rssi<json.req[j].rssi&&json.req[i].ssid===json.req[j].ssid){continue jjj}}var list=document.createElement(\"li\");list.setAttribute(\"onclick\",\"wificonfig('\" + json.req[i].ssid + \"')\");if(json.req[i].rssi>=-50){list.innerHTML+=WiFiFull}else{if(json.req[i].rssi>=-65){list.innerHTML+=WiFiMid}else{if(json.req[i].rssi>=-80){list.innerHTML+=WiFiLow}else{list.innerHTML+=WiFiU}}}list.innerHTML+=\"<span class='ssid_name'>\"+json.req[i].ssid+\"</span>\";if(json.req[i].encryptionType>0){list.innerHTML+=LockSymbol}document.getElementById(\"scan_list\").appendChild(list);btn.style.display=\"none\"}document.getElementById('scan-btn').textContent=\"重新扫描\";document.getElementById('scan-btn').removeAttribute(\"disabled\");document.getElementById('scan-btn').style.opacity=\"1\";flag++}};xml.open(\"GET\",\"/scanWiFi\",true);xml.send()}</script></body></html>"

#endif
